<?php
namespace app\index\controller;
use app\admin\model\Arctile;
use app\admin\model\Banner;
use app\admin\model\Category;
use app\admin\model\Content;
use app\admin\model\Dep_customer;
use app\admin\model\Tag;
use app\admin\model\Tag_correlation;
use app\index\controller\Base;
use think\Db;
use think\Request;
use think\Session;
use think\Cookie;
use Firebase\JWT\JWT;
use app\index\model\Customer;


class Index extends Base
{
    public function _initialize()
    {
        //验证是否免登录
        $redis2 = new \Redis();
        $redis2->connect('127.0.0.1', 6379);
        //token 验证
        $key = "example_key";
        $jwt = Cookie::get('token_jwt');

        //判断token值是否存在
        if(!empty($jwt) && $jwt!== 'null'){
            $obj = JWT::decode($jwt,$key,array('HS256'));
            $rows = Customer::get($obj->id);
            //判断密码是否正确
            if(!empty($rows)){
                $customer = $rows->getData();
                $cus_dep = Dep_customer::get($customer['id']);
                $customer['nickname'] = $cus_dep['name'];
                $customer['figureurl'] = "/static/upload/head/".$cus_dep['headimg'];
                Session::set('user',$customer);
                return;
            }

        }
    }
    //得到栏目
    public function getCate(){
        $cate = Category::all(function($query){
            $query->where('pid', 0)->limit(10);
        });
        $cates = [];
        $num = 0;
        foreach ($cate as $val){
            $pid = $val->getData()['id'];
            $cates[$num] = $val->getData();
            $cates[$num]['cate2'] = Db::name('category')->where('pid', '=', $pid)->select();
            $num ++;
        }
        return $cates;
    }
    //按发布时间来查询
    public function getNewArctile()
    {
        $newArc = [];
        $newArc = Db::name('arctile')->where('status', '=', 1)->limit(20)->order('release_time', 'desc')->select();
        return $newArc;
    }
    //按浏览量条件条件得到文章
    public function getHotArctile()
    {
        $hotArc = [];
        $hotArc = Db::name('arctile')->where('status', '=', 1)->limit(20)->order('browse_num', 'desc')->select();
        return $hotArc;
    }

    //按点赞条件得到文章
    public function getPraiseArctile()
    {
        $praiseArc = [];
        $praiseArc = Db::name('arctile')->where('status', '=', 1)->limit(20)->order('praise_num', 'desc')->select();
        return $praiseArc;
    }

   


    //显示首页
    public function index()
    {
        $cates = $this->getCate();
        $list = Arctile::all(function($query){
            $query->where('status', 1)->limit(10)->order('id', 'desc');
        });
        $tags = Tag::all();
        $banner = Banner::all(function($query){
            $query->where('status', 1)->limit(6);
        });
        $newArc = $this->getNewArctile();
        $hotArc = $this->getHotArctile();
        $praiseArc = $this->getPraiseArctile();
        return view('index/index',['arc'=>$list, 'cate'=>$cates, 'tag'=>$tags, 'banner'=>$banner, 'newArc'=>$newArc, 'hotArc'=>$hotArc, 'praiseArc'=>$praiseArc]);
    }

    public function errorIndex()
    {
        return view('public/error');
    }


    //加载文章，更改浏览量
    public function content(Request $request)
    {
        $cates = $this->getCate();
        $id = $request->param('id');
        $arc = Arctile::get($id);
        $arc->browse_num ++;
        $arc->save();
        $con = Content::where('id', '=',$id)->find();
        $tags = Tag::all();
        $banner = Banner::all(function($query){
            $query->where('status', 1)->limit(6);
        });
        $newArc = $this->getNewArctile();
        $hotArc = $this->getHotArctile();
        $praiseArc = $this->getPraiseArctile();
        return view('arctile/content', ['data'=>$con, 'cate'=>$cates, 'arc'=>$arc, 'tag'=>$tags, 'banner'=>$banner, 'newArc'=>$newArc, 'hotArc'=>$hotArc, 'praiseArc'=>$praiseArc]);
    }

    //点赞统计
    public function praise(Request $request)
    {
        $id = $request->post('id');
        $arc = Arctile::get($id);
        $arc->praise_num ++;
        $res = $arc->save();
        return  $arc->praise_num;
    }
    //取消赞
    public function cutPraise(Request $request){
        $id = $request->post('id');
        $arc = Arctile::get($id);
        $arc->praise_num --;
        $res = $arc->save();
        return  $arc->praise_num;
    }

    //文章分类
    public function category(Request $request){
        $cateid = $request->param('id');
        $cates = $this->getCate();
        $list = Arctile::all(function($query) use($cateid){
            $query->where('status', 1)->where('cateid', $cateid)->limit(10)->order('id', 'desc');
        });
        $tags = Tag::all();
        $banner = Banner::all(function($query){
            $query->where('status', 1)->limit(6);
        });
        $newArc = $this->getNewArctile();
        $hotArc = $this->getHotArctile();
        $praiseArc = $this->getPraiseArctile();
        return view('index/index',['arc'=>$list, 'cate'=>$cates, 'tag'=>$tags, 'banner'=>$banner, 'newArc'=>$newArc, 'hotArc'=>$hotArc, 'praiseArc'=>$praiseArc]);
    }
    //根据标签查看文章
   public function getTagArctile($id)
   {
        $arc_tag = [];
        $arc = [];
        $arc_tag = Db::name('tag_correlation')->where('tagid', '=', $id)->select();
        foreach ($arc_tag as $val){
            $arc[] = Db::name('arctile')->where('id', '=', $val['arcid'])->find();
        }
        return $arc;
   }
   //加载并把标签查询的文章显示到页面上
    public function tagArctile(Request $request)
    {
        $id = $request->param('id');
        $cates = $this->getCate();
        $arc = $this->getTagArctile($id);
        $tags = Tag::all();
        $banner = Banner::all(function($query){
            $query->where('status', 1)->limit(6);
        });
        $newArc = $this->getNewArctile();
        $hotArc = $this->getHotArctile();
        $praiseArc = $this->getPraiseArctile();
        return view('tagArctile/index',['arc'=>$arc, 'cate'=>$cates, 'tag'=>$tags, 'banner'=>$banner, 'newArc'=>$newArc, 'hotArc'=>$hotArc, 'praiseArc'=>$praiseArc]);
    }

    //搜索
    public function search(Request $request)
    {
        $tname = $request->post('tag');
        $tag = [];
        $arc_tag = [];
        $tag = Db::name('tag')->where('tname', 'like', $tname.'%')->select();

       foreach ($tag as $val){
           $arc_tag[] = Db::name('tag_correlation')->where('tagid', '=', $val['id'])->select();
       }
       $arcid = [];
       foreach ($arc_tag as $val){
           foreach ($val as $v) {
               $arcid[] = $v['arcid'];
           }
       }
       
       $searchArc = [];
       foreach ($arcid as $val){
           $searchArc[] = Db::name('arctile')->where('id', '=', $val)->find();
       }
       return view('index/search', ['searchArc'=>$searchArc]);

    }
}
